//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo')
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    // onLoad: function () {
    //     var that = this;
    //     wx.login({
    //         success: function (res) {
    //             if (res.code) {
    //                 that.setData({
    //                     code: res.code
    //                 })
    //             } else {
    //                 console.log('登录失败！' + res.errMsg)
    //             }
    //         }
    //     });
    // },
    getUserInfoAction(res) {
        console.log(res);
        let that = this;
        const encryptedData = res.detail.encryptedData;
        const iv = res.detail.iv;

        if (encryptedData && iv) {
            wx.showLoading({
                title: "正在加载，请稍后",
            })
            wx.login({
                success: function (login) {
                    var params = { "code": login.code, "encryptedData": encryptedData, "iv": iv };
                    app.network.requestPost(app.route.ROUTE_LOGIN_WX_USER, params, '正在加载数据', function (res) {
                        //res就是我们请求接口返回的数据
                        console.log("=====", res.context.userID);
                        if (res.success) {
                            let userinfo = {
                                userID: res.context.userID
                            }
                            wx.setStorageSync("userinfo", userinfo);
                            wx.reLaunch({
                                url: '../index/index'
                            })
                        }
                    }, function () {
                        wx.showToast({
                            title: '加载数据失败',
                        })
                    })

                }
            });

        } else {
            wx.showToast({
                title: '请授权公开信息,登录小程序',
            })
        }

    }
})
